package cn.edu.scau.cmi.zenglinghong.client;

import java.util.Map;
import java.util.HashMap;

import cn.edu.scau.cmi.zenglinghong.composite.safe.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SafeCompositeClient extends Application{
	private static SafeTeam rootSafeTeam = null;
	static Map<String,SafeTeam> hashMap = new HashMap<String,SafeTeam>();
	
	public static void main(String[] args) {
		launch(args);
	}

	public static void init(Stage primaryStage) throws Exception {
		Label label1 = new Label("请输入安全组合模式的根节点：");
		TextField textField1 = new TextField();
		Button button1 = new Button("创建");
		Label label2 = new Label();
		Label label3 = new Label("请输入团队名称及其父节点(用单个空格隔开)");
		TextField textField2 = new TextField();
		Button button2 = new Button("添加");
		Label label4 = new Label();
		Label label5 = new Label("请输入个人名称及其父节点(用单个空格隔开)");
		TextField textField3 = new TextField();
		Button button3 = new Button("添加");
		Label label6 = new Label();
		Button button4 = new Button("显示结构");
		TextArea textArea = new TextArea();
		textArea.setEditable(false);
		
		VBox root = new VBox();
		root.getChildren().add(label1);
		root.getChildren().add(textField1);
		root.getChildren().add(button1);
		root.getChildren().add(label2);
		root.getChildren().add(label3);
		root.getChildren().add(textField2);
		root.getChildren().add(button2);
		root.getChildren().add(label4);
		root.getChildren().add(label5);
		root.getChildren().add(textField3);
		root.getChildren().add(button3);
		root.getChildren().add(label6);
		root.getChildren().add(button4);
		root.getChildren().add(textArea);
		
		Scene scene = new Scene(root);
		root.setSpacing(10);
		primaryStage.setScene(scene);
		primaryStage.setTitle("安全组合模式");
		primaryStage.show();
		
		button1.setOnAction(Handle -> {
			try {
				if(rootSafeTeam == null) {
					rootSafeTeam = new SafeTeam(textField1.getText(), null);
					hashMap.put(textField1.getText(),rootSafeTeam);
					label2.setText("根节点"+textField1.getText()+"创建成功");
				}
				else {
					label2.setText("根节点已创建");
				}
			} catch(Exception e) {
				label2.setText("创建失败");
			}
		});
		
		button2.setOnAction(Handle -> {
			try {
				String[] info = textField2.getText().split(" ");
				if(info.length != 2) throw new Exception("格式错误");
				SafeTeam safeTeam = new SafeTeam(info[0], hashMap.get(info[1]));
				(hashMap.get(info[1])).addChild(safeTeam);
				hashMap.put(info[0],safeTeam);
				label4.setText(info[0]+"添加成功");
			} catch(Exception e) {
				label4.setText("添加失败，请重新检查输入格式");
			}
		});
		
		button3.setOnAction(Handle -> {
			try {
				String[] info = textField3.getText().split(" ");
				if(info.length != 2) throw new Exception("格式错误");
				SafePerson safePerson = new SafePerson(info[0], hashMap.get(info[1]));
				(hashMap.get(info[1])).addChild(safePerson);
				label6.setText(info[0]+"添加成功");
			} catch(Exception e) {
				label6.setText("添加失败，请重新检查输入格式");
			}
		});
		
		button4.setOnAction(Handle -> {
			try {
				textArea.setText(rootSafeTeam.toString());
			} catch(Exception e) {
				textArea.setText("显示失败");
			}
		});
		
//		SafeTeam WXG = new SafeTeam("微信事业群", null);
//		SafePerson boss = new SafePerson("张小龙", WXG);
//		WXG.addChild(boss);
//		SafeTeam WX = new SafeTeam("微信", WXG);
//		SafeTeam WXbus = new SafeTeam("微信事业部", WX);
//		SafePerson boss1 = new SafePerson("刘敏华", WXbus);
//		WXbus.addChild(boss1);
//		SafeTeam WXpay = new SafeTeam("微信支付", WX);
//		SafePerson boss2 = new SafePerson("吴毅", WXpay);
//		WXbus.addChild(boss2);
//		WX.addChild(WXbus);
//		WX.addChild(WXpay);
//		WXG.addChild(WX);
//		System.out.println(WXG.toString());
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		init(primaryStage);
	}

}
